package Test02;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * Created by 14229 on 2020/2/25.
 */
public class Test {
    public static void main(String[] args) throws DocumentException {

        List<Dept> dept = getDepts();
        List<Emp> emp = getEmps(dept);
        int max = 0;
        for (Emp emp1 : emp) {
            max=Math.max(max,emp1.getEmpsalary());
        }
        for (Emp emp1 : emp) {
            if (emp1.getEmpsalary()==max){
                for (Dept dept1 : dept) {
                    if (emp1.getEmpid().equals(dept1.getDeptid())){
                        System.out.println("部门名称："+dept1.getDeptname()+" "+emp1.getEmpname()+" "+emp1.getEmpsalary());
                    }
                }
            }
        }
        List<Map<String, String>> evg = getEvg(dept);
        for (Map<String, String> stringStringMap : evg) {
            System.out.println(stringStringMap);
        }
        searchEmp("e03");


    }
    public static void searchEmp(String empld) throws DocumentException {
        //代码填充
        int count = 0;
        List<Dept> depts = getDepts();
        for (Dept dept : depts) {
            List<Emp> list = dept.getList();
            for (Emp emp : list) {
                if (emp.getEmpid().equals(empld)){
                    System.out.println(emp.getEmpname()+" "+emp.getEmpid());
                    return;
                }else {
                    count++;
                }
            }
        }
        System.out.println("没有找到");
    }
    public static List<Map<String,String>> getEvg(List<Dept> list) throws DocumentException {
        List<Map<String,String>> lsit = new ArrayList<>();
        //代码填充
        int count = 0;
        int sum = 0;
        for (Dept dept : list) {
            System.out.print(dept.getDeptname()+"：");
            List<Emp> list1 = dept.getList();
            for (Emp emp : list1) {
                sum+=emp.getEmpsalary();
                count++;
            }
            System.out.println(sum/count);
        }
        return lsit;
    }
    public static List<Emp> getEmps(List<Dept> list) throws DocumentException {
        List<Emp> emp = new ArrayList<>();
        //代码填充
        int max = 0;
        for (Dept dept : list) {
            List<Emp> list1 = dept.getList();
            for (Emp em : list1) {
                if (max<em.getEmpsalary()){
                    max=em.getEmpsalary();
                }
            }
        }
        for (Dept dept : list) {
            List<Emp> list1 = dept.getList();
            for (Emp em : list1) {
                if (max==em.getEmpsalary()){
                    System.out.println(dept.getDeptname()+" "+em.getEmpname()+" "+em.getEmpsalary());
                }
            }
        }
        return emp;

    }
    public static List<Dept> getDepts() throws DocumentException {
        List<Dept> list = new ArrayList<>();
        //代码填充

        SAXReader sr = new SAXReader();
        Document read = sr.read(new File("dept.xml"));
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String name = element.elementText("dept-name");
            String id = element.elementText("dept-id");
            list.add(new Dept(name,id,new ArrayList<>()));
        }
        List<Emp> emp = new ArrayList<>();
        SAXReader sr1 = new SAXReader();
        Document read1 = sr1.read(new File("emp.xml"));
        Element rootElement1 = read1.getRootElement();
        List<Element> elements1 = rootElement1.elements();
        for (Element element : elements1) {
            String name = element.elementText("emp-name");
            String eid = element.elementText("emp-id");
            String did = element.elementText("dept-id");
            int salary = Integer.parseInt(element.elementText("emp-salary")) ;
            emp.add(new Emp(name,eid,did,salary));
        }
        Set<Emp> set = new HashSet<>(emp);
        emp.clear();
        emp.addAll(set);
        for (Dept dept : list) {
            for (Emp em : emp) {
                if (dept.getDeptid().equals(em.getDeptid())){
                    dept.getList().add(em);
                }
            }
        }
        return list;

    }
}
